package com.siashan.unipay4j.ali.redpack;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.siashan.unipay4j.ali.AbstractAliPay;
import com.siashan.unipay4j.ali.AliConstants;
import com.siashan.unipay4j.core.Result;
import com.siashan.unipay4j.core.redpack.IRedpackSend;
import com.siashan.unipay4j.core.redpack.RedpackModel;
import com.siashan.unipay4j.core.util.StrUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝发放红包
 *
 * @author siashan
 * @since v1.0.1
 **/
public class AliPayRedpackSend extends AbstractAliPay implements IRedpackSend {
    /**
     * 红包发放
     *
     * @param model  请求实体
     * @return 发放结果
     */
    @Override
    public Result sendRedpack(RedpackModel model) {
        AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
        request.setBizContent(JSON.toJSONString(wrapParam2Account(model)));
        try {
            AlipayFundTransUniTransferResponse response = alipayClient.execute(request);
            if (response.isSuccess()) {
                return Result.tradeSuccess(response);
            } else {
                return Result.tradeFail(response.getSubCode(), response.getSubMsg());
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return Result.exception();
    }

    /**
     * 发放裂变红包
     *
     * @param model  请求实体
     * @return  发放结果
     */
    @Override
    public Result sendGroupRedpack(RedpackModel model) {
        return null;
    }

    /**
     * 发放小程序红包
     *
     * @param model  请求实体
     * @return  发放结果
     */
    @Override
    public Result sendMiniprogramRedpack(RedpackModel model) {
        return null;
    }


    /**
     * 封装请求参数
     *
     * @param model  请求实体
     * @return  请求参数
     */
    private Map<String, Object> wrapParam2Account(RedpackModel model) {
        Map<String, Object> map = new HashMap<>(8);
        map.put("out_biz_no", model.getOutTradeNo());
        map.put("trans_amount", model.getTotalAmount());
        map.put("product_code", AliConstants.ProductCode.STD_RED_PACKET);
        map.put("biz_scene", AliConstants.BizScene.DIRECT_TRANSFER);
        map.put("remark", model.getRemark());
        if (StrUtil.isNotEmpty(model.getActName())) {
            map.put("order_title", model.getActName());
        }
        Map<String, Object> payeeInfo = new HashMap<>(4);
        payeeInfo.put("identity", model.getOpenid());
        payeeInfo.put("identity_type", com.siashan.unipay4j.core.transfer.IdentityType.USER_ID.equals(model.getIdentityType()) ? AliConstants.IdentityType.ALIPAY_USER_ID : AliConstants.IdentityType.ALIPAY_LOGON_ID);
        if (StrUtil.isNotEmpty(model.getReUserName())) {
            payeeInfo.put("name", model.getReUserName());
        }
        map.put("payee_info", payeeInfo);
        return map;
    }
}
